package com.demodis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.demodis.dao.DeterGroupDaoI;
import com.demodis.dao.UserDaoI;
import com.demodis.dao.impl.DeterGroupDaoImpl;
import com.demodis.dao.impl.UserDaoImpl;
import com.demodis.domain.TblDeterGroup;
import com.demodis.domain.TblUser;
import com.demodis.service.DeterGroupServiceI;

public class DeterGroupServiceImpl implements DeterGroupServiceI {

	DeterGroupDaoI deterDao = new DeterGroupDaoImpl();
	UserDaoI userDao = new UserDaoImpl();

	@Override
	public Object getDeterGroupsRCU(TblUser user) {
		List<TblDeterGroup> deterList = deterDao.getDeterGroupsRCU(user);
		Map<String, Object> json = new HashMap<String, Object>();
		List<Object> list = new ArrayList<Object>();
		Map<String, Object> map = null;
		for (TblDeterGroup deter : deterList) {
			map = new HashMap<String, Object>();
			map.put("dgId", deter.getDgId());
			map.put("groupId", deter.getGroupId());
			map.put("detersubjCont", deter.getDetersubjCont());

			map.put("groupName", deter.getGroupName());
			list.add(map);
		}
		json.put("total", deterList.size());
		json.put("rows", list);
		json.put("model", "do");

		return json;
	}
	
	//add by robbie
	@Override
	public Object getManageGroups(TblUser user) {
		List<TblDeterGroup> deterList = deterDao.getDeterManagerGroups(user);

		return deterList;
	}
	
	
	@Override
	public Object getDeterGroups() {
		List<TblDeterGroup> deterList = deterDao.getDeterGroups();

		return deterList;
	}

	@Override
	public Object getDeterGroupsCU(TblUser user) {
		List<TblDeterGroup> deterList = deterDao.getDeterGroupsCU(user);

		return deterList;
	}

	@Override
	public boolean save(TblDeterGroup deterGroup) {
		return deterDao.save(deterGroup);
	}

	@Override
	public boolean modify(TblDeterGroup deterGroup) {
		return deterDao.modify(deterGroup);
	}

	@Override
	public boolean delete(TblDeterGroup deterGroup) {
		return deterDao.delete(deterGroup);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean doDeter(TblDeterGroup deterGroup, TblUser user) {
		List<HashMap> list =  JSON.parseArray(deterGroup.getDeterStr(), HashMap.class);
		for(Map<String, String> map : list) {
			deterGroup.setDgId(Integer.parseInt(map.get("dgId")));
			int value = Integer.parseInt(map.get("value"));
			//如果有恶意分数（超过15分），那么所评分数为该值的负值
			deterGroup.setDeterRes(value > 15 ? value * -1 : value);
			deterDao.doDeter(deterGroup); 
		}
		//userDao.modifyDetered(user.getUserId(), 1);
		return true;
	}


	
	/**
	 * 初始化TblDeterPerson表    清除以前的测评结果  （测评结果清零） 
	 */
	public void initTblDeterPerson() {
		/*List<TblDeterGroup> deterList = deterDao.getDeterGroups();
		for(TblDeterGroup d : deterList) {
			StringBuffer sb = new StringBuffer();
			String[] opts = d.getDeterRes().split(",");
			for(String str : opts) {
				str = str.split("=")[0];
				if (str.trim().length() != 0) {
					if (sb.toString().length() == 0) {
						sb.append(str + "=0");
					} else {
						sb.append("," + str + "=0");
					}
				}
			}
			d.setDeterRes(sb.toString());
			System.out.println(d.getDeterRes());
			deterDao.doDeter(d);
		}
		deterDao.initTblDeterPerson();*/
	}

}
